#!/usr/bin/python

import csv
import cfg

def get_arr_key_val(arr, key, def_val):
    if key < len(arr):
        return arr[key]
    return def_val

def get_tab_key_val(tab, key, def_val):
    if key in tab:
        return tab[key]
    return def_val

def stof(var):
    return var != "" and float(var) or 0.0

def add_if(key, tab, fvar, def_var):
    if key in tab:
        return tab[key] + fvar
    return fvar + def_var

class Reader(object):
    def __init__(self):
        # for [week,"",local_reg] data
        self._week_result =  cfg.init_input_tab()
        # year's acc_week result for [year,"",local_reg] data
        self._acc_year_result =  cfg.init_input_tab()
        # the final struct for all data item, trade_line existing in the 2015 or 2016
        self._all_result =  cfg.init_result_tab()
        
    def _read(self, reader):
        pass

    def read(self, reader):
        self._read(reader)
        return (self._week_result, self._acc_year_result, self._all_result)


def read_csv(data_source, reader):
    with open(data_source, 'rt') as csvfile:
        csv_reader = csv.reader(csvfile)
        return reader.read(csv_reader)


def write_file(fn, str_arr):
    fp = open(fn, "w+")
    for i in str_arr:
        fp.write(i)
    fp.close()


